<?php
declare (strict_types = 1);

namespace app\common\model;

use think\Model;
use think\response\Json;

/**
 * @mixin \think\Model
 */
class SeckillProduct extends Base
{

    protected $name = 'seckill_product';
    
    public function product()
    {
        return $this->hasOne(Product::class, 'id', 'product_id')->field('id,title,icon,kill_price,price,vip_price');
    }
    public static function saveProduct($param)
    {
        parent::startTrans();
        try {
            unset($param['name']);
            if (isset($param['id']) && $param['id']) {
                $product = parent::update($param);
                $product->operation = 'save';
            } else {
                $find=parent::where(['seckill_id'=>$param['seckill_id'],'product_id'=>$param['product_id'],'is_del'=>0])->find();
                if(!empty($find)){
                    return ['code' => -1, 'msg' => '该商品已在秒杀列表！'];
                }
                $product = parent::create($param);
                $product->operation = 'create';
            }

            //设置商品规格数据
            $product->save();

            parent::commit();
            return ['code' => 1, 'msg' => '操作成功！'];
        } catch (\Exception $e) {
            return ['code' => -1, 'msg' => $e->getMessage()];
        }
    }
}
